<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;

use cmf\controller\RestUserBaseController;
use think\Db;
use think\Validate;

//业务员控制器
class MerchandiserController extends RestUserBaseController
{


    //  添加企业注册登记表;
    public function addRegister()
    {
        $role = $this->getUserRole();   //  获取该用户角色

        if (!($role == '业务员' || $role == '技术员')) $this->error("非业务员或技术员不能添加!");



        $validate = new Validate([
            'company_name' => 'require',
        ]);

        $validate->message([
            'company_name.require' => '请输入项目名字!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }


        $register_stock_civics = $data['register_stock_civics'];
        $register_stock_legal  = $data['register_stock_legal'];

        $data['product']       = $data['product'] ? json_encode($data['product']) : '';
        $data['register_type'] = $data['register_type'] ? json_encode($data['register_type']) : '';


        unset($data['register_stock_civics']);
        unset($data['register_stock_legal']);



        $id = Db::name('project_register')->insertGetId($data);

//            dump($id);
        if (!$id) $this->error("数据异常无法添加!");

            //添加如果公司名称字段真就添加公司名称到 preject
            $project_data['company_name'] = $data['company_name'];
            Db::name('project')
                ->where('id', $data['project_id'])
                ->update($project_data);


        if ($register_stock_civics) {
            // $register_stock_civics     = htmlspecialchars_decode($register_stock_civics);
            // $register_stock_civics     = json_decode($register_stock_civics);
            $new_register_stock_civics = [];

            foreach ($register_stock_civics as $key => &$val) {
                //$val                             = $this->object_to_array($val);
                if ($val) {
                    $val['register_id']              = $id;
                    $new_register_stock_civics[$key] = $val;
                }

            }
            unset($val);

            $civics_num = Db::name('register_stock_civics')->insertAll($new_register_stock_civics);

        }

        if ($register_stock_legal) {
            //  $register_stock_legal     = htmlspecialchars_decode($register_stock_legal);
            //  $register_stock_legal     = json_decode($register_stock_legal);
            $new_register_stock_legal = [];

            foreach ($register_stock_legal as $key => &$val) {
                //$val                            = $this->object_to_array($val);
                if ($val) {
                    $val['register_id']             = $id;
                    $new_register_stock_legal[$key] = $val;
                }

            }
            unset($val);

            $legal_num = Db::name('register_stock_legal')->insertAll($new_register_stock_legal);
        }


        $this->success("添加企业注册登记成功!");


//        [{"register_id":"1","civics_type_id":"2","stock_name":"股权名称","stock_num":"股权身份证","stock_investment":"股权投资额"},{"register_id":"1","civics_type_id":"2","stock_name":"股权名称","stock_num":"股权身份证","stock_investment":"股权投资额"}]

//         [{"register_id":"1","legal_type_id":"3","stock_legal_name":"法人名称","stock_legal_num":"法人代码","stock_legal_investment":"法人投资额"},{"register_id":"1","legal_type_id":"3","stock_legal_name":"法人名称","stock_legal_num":"法人代码","stock_legal_investment":"法人投资额"}]


    }


    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)object_to_array($v);
            }
        }

        return $obj;
    }


    //  添加项目之主情况表;
    public function addCondition()
    {
        $role = $this->getUserRole();   //  获取该用户角色

        if (!($role == '业务员' || $role == '技术员')) $this->error("非业务员或技术员不能添加!");

        $data = $this->request->param();

        $id = Db::name('project_condition')->insertGetId($data);
        if (!$id) $this->error("服务器繁忙!");

        $this->success("添加主情况成功!");


    }

    //  添加知识产权汇总表;
    public function addKnowledge()
    {
        $role = $this->getUserRole();   //  获取该用户角色

        if (!($role == '业务员' || $role == '技术员')) $this->error("非业务员或技术员不能添加!");


        $data = $this->request->param();

        $knowledge_info      = $data['knowledge_info'];
        $knowledge_condition = $data['knowledge_condition'];

        if ($data['one_circuit_design']) {
            $data['one_circuit_design'] = json_encode($data['one_circuit_design']);
        }

        if ($data['one_invent_patent']) {
            $data['one_invent_patent'] = json_encode($data['one_invent_patent']);
        }

        if ($data['one_defence_variety']) {
            $data['one_defence_variety'] = json_encode($data['one_defence_variety']);
        }

        if ($data['one_plant_variety']) {
            $data['one_plant_variety'] = json_encode($data['one_plant_variety']);
        }

        if ($data['one_medicine_variety']) {
            $data['one_medicine_variety'] = json_encode($data['one_medicine_variety']);
        }

        if ($data['one_crop_variety']) {
            $data['one_crop_variety'] = json_encode($data['one_crop_variety']);
        }

        if ($data['one_new_medicine']) {
            $data['one_new_medicine'] = json_encode($data['one_new_medicine']);
        }

        if ($data['two_software_copyright']) {
            $data['two_software_copyright'] = json_encode($data['two_software_copyright']);
        }

        if ($data['two_facade_variety']) {
            $data['two_facade_variety'] = json_encode($data['two_facade_variety']);
        }


        unset($data['knowledge_info']);
        unset($data['knowledge_condition']);

        $id = Db::name('project_knowledge')->insertGetId($data);
        if (!$id) $this->error("数据异常无法添加!");

        if ($knowledge_info) {
            //$knowledge_info     = htmlspecialchars_decode($knowledge_info);
            //$knowledge_info     = json_decode($knowledge_info);
            $new_knowledge_info = [];
            //
            foreach ($knowledge_info as $key => &$val) {

                //$val                      = $this->object_to_array($val);

                $val['info_accessory']    = $val['info_accessory'] ? json_encode($val['info_accessory']) : '';
                $val['knowledge_id']      = $id;
                $new_knowledge_info[$key] = $val;
            }
            unset($val);
            $civics_num = Db::name('knowledge_info')->insertAll($new_knowledge_info);
        }

        if ($knowledge_condition) {

            //$knowledge_condition     = htmlspecialchars_decode($knowledge_condition);
            //$knowledge_condition     = json_decode($knowledge_condition);
            $new_knowledge_condition = [];

            foreach ($knowledge_condition as $key => &$val) {
                //$val                           = $this->object_to_array($val);


                $val['condition_accessory']    = $val['condition_accessory'] ? json_encode($val['condition_accessory']) : '';
                $val['knowledge_id']           = $id;
                $new_knowledge_condition[$key] = $val;
            }
            unset($val);
            $legal_num = Db::name('knowledge_condition')->insertAll($new_knowledge_condition);

        }


        $this->success("添加主情况成功!");


//        [{"knowledge_id":"3","intellectual_property_number":"3","intellectual_property_name":"知识产权名称","knowledge_info_type_id":"1","privileged_time":"授权时间","privileged_num":"授权号","access":"获取方式","intellectual_property_status":"0","info_accessory":"附件"}]
//        [{"knowledge_id":"3","standard_name":"标准名称","standard_level_id":"知识产权名称","standard_num":"标准编号","standard_time":"标准时间","participation_way":"参与方式","condition_accessory":"附件"}]


    }

    //  获取项目类型表关联数组;
    public function addProject()
    {
        $role = $this->getUserRole();   //  获取该用户角色

        if (!($role == '业务员' || $role == '技术员')) $this->error("非业务员或技术员不能添加!");

        $validate = new Validate([
            'project_type_id' => 'require',
        ]);

        $validate->message([
            'project_type_id.require' => '请选择您的分类!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $project_type_data = Db::name('project_type')
            ->field('l_id')
            ->where('id', $data['project_type_id'])
            ->find();

        $project_type_arr = explode(',',$project_type_data['l_id']);
        if (!$project_type_data['l_id']) $this->error("服务器繁忙!");

        $this->success("获取项目类型表关联数组成功!", $project_type_arr);


    }

    //  修改项目表;
    public function upProject()
    {
        $role = $this->getUserRole();   //  获取该用户角色

        if (!($role == '业务员' || $role == '技术员')) $this->error("非业务员或技术员不能添加!");

        $validate = new Validate([
            'project_id' => 'require',

        ]);

        $validate->message([
            'project_id.require' => '请选择您的分类!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data['yewu_id'] = $this->getUserId();
//        $data['create_time'] = time();
        $data['shenhe_id'] = 2;

        $id = Db::name('project')->where('id', $data['id'])->update($data);
        if (!$id) $this->error("服务器繁忙!");

        $this->success("新增项目成功!", ['project_id' => $id]);


    }


}
